package com.meihua.shellapp.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aspsine.irecyclerview.IRecyclerView;
import com.aspsine.irecyclerview.OnLoadMoreListener;
import com.aspsine.irecyclerview.OnRefreshListener;
import com.meihua.shellapp.dialog.LoadingDialog;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment implements View.OnClickListener, OnLoadMoreListener, OnRefreshListener {
    protected View rootView;
    private boolean isInitView;
    private boolean isLoad;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null)
            rootView = inflater.inflate(getLayoutResource(), container, false);
        ButterKnife.bind(this, rootView);
        initView();
        isInitView = true;
        isCanLoadData();
        createLoadData();
        return rootView;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isCanLoadData();
    }

    public void isCanLoadData() {
        if(getUserVisibleHint()&&isInitView&&!isLoad){
            loadData();
           isLoad = true;
        }
    }

    /**\
     * 加载数据 后期优化
     */
    public void loadData() {

    }

    public void createLoadData() {
    }

    //获取布局文件
    protected abstract int getLayoutResource();

    //初始化view
    protected abstract void initView();
    private LoadingDialog loadingDialog;
    /**
     * 显示load框
     */
    public void showLoadingDialog() {
        if (loadingDialog == null){
            loadingDialog = new LoadingDialog();
        }
        if (!loadingDialog.isVisible()) {
            loadingDialog.show(getFragmentManager(), getClass().getSimpleName());
        }
    }
    /**
     * 隐藏load框
     */
    public void dismissLoadingDialog() {
        if (loadingDialog == null) {
            return;
        }
        loadingDialog.dismiss();
        loadingDialog = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        loadingDialog = null;
        isInitView = false;
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View view) {
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onRefresh() {

    }

    public void closeRefreshing(IRecyclerView view) {
        view.setRefreshing(false);
        ((LoadMoreFooterView) view.getLoadMoreFooterView()).setStatus(LoadMoreFooterView.Status.GONE);
    }

    public void endRefreshing(IRecyclerView view) {
        try {
            if (null != view.getLoadMoreFooterView()) {
                ((LoadMoreFooterView) view.getLoadMoreFooterView()).setStatus(LoadMoreFooterView.Status.THE_END);
            }
        }catch (Exception e){}
    }
}
